
package com.epam.android.training.day6;

import com.epam.android.training.SecretContacts;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class SecretContactsActivity extends ListActivity {

    private static final String MIME_TYPE_TEXT = "plain/text";
    private static String[] PROJECTION = new String[] {
            SecretContacts._ID, SecretContacts.NAME, SecretContacts.MAIL
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Cursor cur = managedQuery(SecretContacts.CONTENT_URI, PROJECTION, null, null, null);

        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
                R.layout.list_item,
                cur,
                new String[] {
                        SecretContacts.NAME, SecretContacts.MAIL
                },
                new int[] {
                        R.id.name_text, R.id.phone_text
                });

        setListAdapter(adapter);
        ListView lv = getListView();
        lv.setTextFilterEnabled(true);

        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                final Intent emailIntent = new Intent(Intent.ACTION_SEND);
                emailIntent.setType(MIME_TYPE_TEXT);
                emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] {
                        ((TextView) view.findViewById(R.id.phone_text)).getText().toString()
                });
                startActivity(Intent.createChooser(emailIntent, getResources().getString(R.string.send_mail)));
            }
        });
    }
}
